﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Controllers.Business.BusinessModel;
using FCTC_Web.Utilities;
using FCTC_Web.Models;

namespace FCTC_Web.Controllers.Business
{
    public class VehicleBusinessController : Controller
    {
        public void SetSearchEngine(string CarBrand, string BrandType, string VehicleStyle, string gears, string miles, string age, string prize)
        {
            var ns = VehicleXmlHelper.Brands;
            ViewBag.CarBrand = new SelectList(ns, CarBrand);
            if (!string.IsNullOrWhiteSpace(CarBrand))
                ViewBag.BrandType = new SelectList(VehicleXmlHelper.GetSelectiosn(CarBrand), BrandType);
            else
                ViewBag.BrandType = new SelectList(new List<string>());

            if (!string.IsNullOrWhiteSpace(BrandType))
                ViewBag.VehicleStyle = new SelectList(VehicleXmlHelper.GetvehicleType(CarBrand, BrandType), VehicleStyle);
            else
                ViewBag.VehicleStyle = new SelectList(new List<string>());

            if (!string.IsNullOrWhiteSpace(gears))
                ViewBag.gears = new SelectList(new List<string> { "手动档", "自动档", "手自一体" }, gears);
            else
                ViewBag.gears = new SelectList(new List<string> { "手动档", "自动档", "手自一体" });

            if (!string.IsNullOrWhiteSpace(miles))
                ViewBag.miles = new SelectList(
                    new List<string> { "1万公里以内", "3万公里以内", "5万公里以内", "10万公里以内", "20万公里以内" }, miles);
            else
                ViewBag.miles = new SelectList(
                    new List<string> { "1万公里以内", "3万公里以内", "5万公里以内", "10万公里以内", "20万公里以内" });

            if (!string.IsNullOrWhiteSpace(age))
                ViewBag.age = new SelectList(
                                   new List<string> { "1年以内", "3年以内", "5年以内", "10年以内", "20年以内" }, age);
            else
                ViewBag.age = new SelectList(
                                   new List<string> { "1年以内", "3年以内", "5年以内", "10年以内", "20年以内" });

            if (!string.IsNullOrWhiteSpace(prize))
                ViewBag.prize = new SelectList(
                          new List<string> { "3万以内", "5万以内", "10万以内", "20万以内", "30万以内", "50万以内" }, prize);
            else
                ViewBag.prize = new SelectList(
                    new List<string> { "3万以内", "5万以内", "10万以内", "20万以内", "30万以内", "50万以内" });
        }

        public ActionResult Index(int? pageId, string CarBrand, string BrandType, string VehicleStyle, string gears, string miles, string age, string prize)
        {
            SetSearchEngine(CarBrand, BrandType, VehicleStyle, gears, miles, age, prize);
            var searchString = "?CarBrand=" + CarBrand + "&&BrandType=" + BrandType + "&&VehicleStyle=" + VehicleStyle + "&&gears=" + gears
                + "&&miles=" + miles + "&&age=" + age + "&&prize=" + prize;
            ViewBag.Search = searchString;

            VehicleBusiness.Ins.VehicleSearchRearchResult = Pager10.Ins.GetPage<Vehicle>(pageId, i => i.CarBrand.Contains(CarBrand == null ? string.Empty : CarBrand)
                && i.BrandType.Contains(BrandType == null ? string.Empty : BrandType)
                && i.VehicleStyle.Contains(VehicleStyle == null ? string.Empty : VehicleStyle)
                && CheckString(i.Gears, gears)
                && CheckDoubleValue(i.Miles, miles, "万公里以内")
                && CheckIntValue(i.VehicleAge, age, "年以内")
               && CheckDecimal(i.Prize, prize, "万以内")
               && i.VehicleStatus == "热卖");

            return View(@"~\views\business\vehicles\VehicleExchangePage.cshtml", VehicleBusiness.Ins);
        }

        bool CheckString(string origin, string search)
        {
            if (string.IsNullOrWhiteSpace(search)) return true;
            if (origin != search)
                return false;
            return true;
        }

        bool CheckDoubleValue(double? origin, string search, string replace)
        {
            if (string.IsNullOrWhiteSpace(search)) return true;
            if (!string.IsNullOrWhiteSpace(search) && origin != null)
            {
                if (Convert.ToDouble(search.Replace(replace, "")) < origin)
                    return false;
            }

            return true;
        }

        bool CheckIntValue(int? origin, string search, string replace)
        {
            if (string.IsNullOrWhiteSpace(search)) return true;
            if (!string.IsNullOrWhiteSpace(search) && origin != null)
            {
                if (Convert.ToDouble(search.Replace(replace, "")) < origin)
                    return false;
            }

            return true;
        }

        bool CheckDecimal(decimal? origin, string search, string replace)
        {
            if (string.IsNullOrWhiteSpace(search)) return true;
            if (!string.IsNullOrWhiteSpace(search) && origin != null)
            {
                if (Convert.ToDecimal(search.Replace(replace, "")) < origin)
                    return false;
            }

            return true;
        }

        public ActionResult VehicleImages(int? id, string brand, string style, string selection)
        {
            SetSearchEngine(brand, style, selection, null, null, null, null);
            return View(@"~\views\business\vehicles\VehicleDetailsPageImgs.cshtml", new VehicleDetailBusiness(id));
        }

        public ActionResult Details(int? id, string brand, string style, string selection)
        {
            SetSearchEngine(brand, style, selection, null, null, null, null);
            return View(@"~\views\business\vehicles\VehicleDetailsPageIndex.cshtml", new VehicleDetailBusiness(id));
        }
    }
}
